﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL.Entities.Event;
using System.Xml.Serialization;
using System.Xml;
using DLL;
using System.IO;
using System.Timers;

namespace COM
{
    public partial class Form1 : Form
    {
        string XMLdir = "..\\..\\..\\DLL\\XML\\";
        string Logdir = "..\\..\\..\\DLL\\Logs\\";
        RADEvent target;
        COMEvent targetpackage;
        MISEvent result;

        int id, speed, altitude, heading, distance;

        public Form1()
        {
            InitializeComponent();
            object test = new object();
            Com config_Com = new Com();
            
            XmlSerializer serializer = new XmlSerializer(typeof(Comm)); 
            XMLHandler.ReadXML( "COM_config.xml", serializer, );
            //config_Com = (Com)test;

            //set version and model type to textbox
            txtboxModel.Text = config_Com.ModelType;
            txtboxVersion.Text = config_Com.Version;
            
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            //Setting text box to offline
            txtbxMisStatus.Text = "OFFLINE";
            txtbxRadStatus.Text = "OFFLINE";

            //Deserializes Com_Config.xml to get the Current Model and Version
            //Display online or offline in textbox.
            Com info = new Com();
            TextReader reader = null;
            try
            {
                //1
                reader = new StreamReader("../../XML/Com_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Com));
                info = (Com)serializer.Deserialize(reader);

                reader.Close();
                //2
                txtbxRadStatus.Text = Convert.ToString(info.ModelType);
                txtbxMisStatus.Text = Convert.ToString(info.Version);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        
        





        
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeUpdate RadTime = new TimeUpdate();
            object test = new object();

            XmlSerializer serializer = new XmlSerializer(typeof(TimeUpdate));
            WriteXML.ReadXML( "RAD_Status.xml", serializer);
            RadTime = (TimeUpdate)test;


            if ((DateTime.Now.Second == RadTime.timestamp.Second) || (DateTime.Now.Second == RadTime.timestamp.Second + 1) || (DateTime.Now.Second == RadTime.timestamp.Second + 2) || (DateTime.Now.Second == RadTime.timestamp.Second + 3))
            {
                txtbxRadStatus.Text = "ON-LINE";
            }
            else
            {
                txtbxRadStatus.Text = "OFF-LINE";
            }
            
            TimeUpdate MisTime = new TimeUpdate();
            object test2 = new object();

            XmlSerializer serializer2 = new XmlSerializer(typeof(TimeUpdate));
            XMLHandler.ReadXML( "RAD_Status.xml", serializer);
            MisTime = (TimeUpdate)test2;


            if ((DateTime.Now.Second == MisTime.timestamp.Second) || (DateTime.Now.Second == MisTime.timestamp.Second + 1) || (DateTime.Now.Second == MisTime.timestamp.Second + 2) || (DateTime.Now.Second == MisTime.timestamp.Second + 3))
            {
                txtbxMisStatus.Text = "ON-LINE";
            }
            else
            {
                txtbxMisStatus.Text = "OFF-LINE";
            }
        
        }




        private void timer2_Tick(object sender, EventArgs e)
        {

            target = new RADEvent();
            if (File.Exists(XMLdir + "RADEvent.xml"))
            {
                try
                {
                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(RADEvent));
                    WriteXML.ReadXML(out test, "RADEvent.xml", serializer);
                    target = (RADEvent)test;

                    File.Move((XMLdir + "RAD_Event.xml"), (Logdir + "RAD_Event" + UtilityEX.Random(1, 10000000) + ".xml"));

                    txt_Target.Text = "Target ID: " + target.id + "\r\n" +
                                          "Size: " + target.size + "\r\n" +
                                          "Speed: " + target.speed + " kn\r\n" +
                                          "Altitude: " + target.altitude + " ft\r\n" +
                                          "Heading: " + target.heading + " degrees\r\n" +
                                          "Distance: " + target.distance + " miles\r\n";

                    id = target.id;
                    speed = target.speed;
                    altitude = target.altitude;
                    heading = target.heading;
                    distance = target.distance;

                    if (target.altitude < 20)
                    {
                        txt_Target.Text = "Ship";
                    }
                    else if (target.altitude >= 20 && target.altitude < 50)
                    {
                        txt_Target.Text = "Birds";
                    }
                    else if (target.altitude >= 50 && target.size == "small")
                    {
                        txt_Target.Text = "Missile";
                    }
                    else
                    {
                        txt_Target.Text = "Aircraft";
                    }

                    btn_Fire.Enabled = true;

                }

                catch (Exception ex)
                {
                    UtilityEX.LogFile(ex.ToString());
                }
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            result = new MISEvent();
            if (File.Exists(XMLdir + "MIS_Event.xml"))
            {
                try
                {

                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(MISEvent));
                    Read_Write.ReadXML(out test, "MIS_Event.xml", serializer);
                    result = (MISEvent)test;

                    txt_Result.Text = "Target ID: " + result.id + "     " + "Result: " + result.outcome;
                    timer3.Stop();

                    File.Move((XMLdir + "MIS_Event.xml"), (Logdir + "MIS_Event" + Utility.Random(1, 10000000) + ".xml"));
                    btn_Abort.Enabled = false;

                    if (result.outcome == "Intercept Failed" || result.outcome == "Intercept Aborted")
                    {

                        btn_Fire.Enabled = true;
                    }
                    else
                    {
                        txt_TargetInfo.Text = "";
                        timer2.Start();
                    }


                }
                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                }

                //txt_Result.Text = "Target ID: " + outcome.id + "    " + outcome.outcome;


            }
        }





        private void StatusEvent(object source, ElapsedEventArgs e)
        {
            {
                Missile MisStatus = new Missile();
                TextReader MisRead = null;
                Radar RadarStat = new Radar();
                TextReader RadRead = null;
                try
                {
                    //Deserializes MIS_Config.xml to get the status of the MIS system
                    //Deserializes RAD_Status.xml to get the status of the RAD system
   
                    MisRead = new StreamReader("../../../DLL/XML/MIS_Status.xml");
                    XmlSerializer MISserializer = new XmlSerializer(typeof(Missile));
                    MisStatus = (Missile)MISserializer.Deserialize(MisRead);

                    MisRead.Close();
                    txtbxMisStatus.Text = "Ready";
                                        
                    RadRead = new StreamReader("../../../RAD/XML/Config.xml");
                    XmlSerializer Radserializer = new XmlSerializer(typeof(Radar));
                    RadarStat = (Radar)Radserializer.Deserialize(RadRead);

                    RadRead.Close();
                    txtbxRadStatus.Text = "Ready";

                }


                catch (XmlException xe)
                {
                    MessageBox.Show(xe.Message, "XML Parse Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

/*        private void Fire_Btn_Click(object sender, EventArgs e)
        {
            Target T = new Target();
            //Serializes Target object with Comm information and creates an event file called Com_Event 
            T.Distance = Convert.ToString(txtboxDistance);
            T.Size = Convert.ToInt32(txtboxSize.Text);
            T.Altitude = Convert.ToInt32(txtboxAltitude.Text);
            T.Direction = Convert.ToString(txtboxDirection.Text);
           
            
            XmlSerializer serializer = new XmlSerializer(typeof(Target));
            TextWriter tw = new StreamWriter("../../XML/COM_Event.xml");
            serializer.Serialize(tw, T);
            tw.Close();

            //arthur dont delete this.
            string location = "";
            T = (Target)XMLHandler.ReadXML(location, serializer);
//            txtboxDistance = T.Distance;
        }
*/










        
    }
}
